package com.chinatgl.latte.ec.launcher;

import android.accounts.AccountManager;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;
import android.widget.TextView;


import com.chinatgl.latte.delegates.LatteDelegate;
import com.chinatgl.latte.ec.R;
import com.chinatgl.latte.ec.R2;
import com.chinatgl.latte.ec.contains.Contains;
import com.chinatgl.latte.ec.main.EcBottomDelegate;
import com.chinatgl.latte.ec.sign.SignInDelegate;
import com.chinatgl.latte.ec.sign.SignUpDelegate;
import com.chinatgl.latte.util.storage.SpUtil;
import com.chinatgl.latte.util.timer.BaseTimerTask;
import com.chinatgl.latte.util.timer.ITimerListener;

import java.text.MessageFormat;
import java.util.Timer;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 文件描述: 启动界面
 * 作者: Created by 杨伟乔 on 2017/8/8 0008.
 * 版本号: 1.0
 */

public class LauncherDelegate extends LatteDelegate implements ITimerListener {
    //倒计时 显示
    @BindView(R2.id.tv_launcher_time)
    AppCompatTextView tvLauncherTime = null;
    //计时2s
    private int mCount = 2;
    private Timer mTimer = null;

    //设置布局
    @Override
    public Object setLayout() {
        return R.layout.delegate_launcher;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        initTimer();
    }

    //    @Override
//    public void onAttach(Activity activity) {
//        super.onAttach(activity);
//        if (activity instanceof ILauncherListener) {
//            mILauncherListener = (ILauncherListener) activity;
//        }
//    }

    /**
     * 初始化timer
     */
    private void initTimer() {
        mTimer = new Timer();
        final BaseTimerTask timerTask = new BaseTimerTask(this);
        mTimer.schedule(timerTask, 0, 1000);
    }

    /**
     * 点击右上角可以提前结束倒计时
     */
    @OnClick(R2.id.tv_launcher_time)
    void OnClick() {
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
            checkIsShowScroll();
        }
    }


    @Override
    public void onTimer() {
        //倒计时结束的回调
        getProxyActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (tvLauncherTime != null) {
                    tvLauncherTime.setText(MessageFormat.format("跳过\n{0}s", mCount));
                    mCount--;
                    if (mCount < 0) {
                        if (mTimer != null) {
                            mTimer.cancel();
                            mTimer = null;
                            System.out.println("倒计时结束");
                            checkIsShowScroll();
                        }
                    }
                }
            }
        });
    }

    /**
     * 判断是否显示滑动页面
     */
    private void checkIsShowScroll() {
        if (!SpUtil.getBoolean(Contains.IS_FRIST)) { //默认是false
            //启动引导界面
            getSupportDelegate().startWithPop(new LauncherScrollDelegate());
        } else {
            //直接进入主界面
            getSupportDelegate().startWithPop(new EcBottomDelegate());
        }
    }

}
